// components/searchView/searchView.js
var searchVal;
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    mykey:{
        type:String,
        value:"",
        observer(newVal){
          searchVal=newVal;
          this.onClickSeach();
        }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    autoFocus:false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onChange(e){
       searchVal = e.detail
      this.onClickSeach()
    },
    onClickSeach(){
      wx.showLoading({
        title: '数据加载中...',
      })
      wx.cloud.callFunction({
        name:"xiangcun_search_get",
        data:{
          keyword:searchVal
        }
      }).then(res=>{
        this.triggerEvent("keyword",searchVal)
        this.triggerEvent("mysearch",res.result.data)
        wx.hideLoading()
      })
    },


    getRoute(){
      var pages = getCurrentPages();//获取页面栈
      var curPage = pages[pages.length-1]
      var url = curPage.route
      var arr = url.split("/")//根据"/"进行分拆成数组
      var route  = arr[arr.length-1]//当前页面的名称
      return route
    },
    onFocus(){
      var route = this.getRoute()
      if(route!="organSearch")
      {
          wx.navigateTo({
          url: '/pages/organSearch/organSearch',
        })
      }
      
    }
  },
  lifetimes:{
    attached(){
      searchVal:"";
      var route = this.getRoute()
      if(route=="organSearch")
      {
        this.setData({
          autoFocus:true
        })
      }
    }
  }
})
